package com.san.report.dao;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.san.base.dao.BaseManager;
import com.san.common.datasource.JdbcManager;
import com.san.common.utils.Constant;
import com.san.metadata.domain.DataSourceDO;
import com.san.report.vo.LegalPersonReportBaseVO;

@Service
public class LegalPersonReportManager extends BaseManager {
	
	private static final Logger logger = LoggerFactory.getLogger(LegalPersonReportManager.class);

	/**
	 * 查询法人信用报告
	 * 
	 * @param busiTypeNode  业务元数据大类ID
	 * @param legalPersonId 法人ID
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> Detail(Long busiTypeNode, String legalPersonId) throws Exception {

		JdbcManager jdbcManager = new JdbcManager();
		List<Map<String, Object>> result = null;

		Object[] obj = { legalPersonId,busiTypeNode };
		StringBuffer querySQL = new StringBuffer();
		querySQL.append("SELECT\r\n")
						.append("	mbd.busi_id,\r\n")
						.append("	mbd.busi_name,\r\n")
						.append("	mbd.algorithm_type,\r\n")
						.append("	mbd.interface_name,\r\n")
						.append("	mbd.link_way,\r\n")
						.append("	rlp.busi_value\r\n")
						.append("FROM\r\n")
						.append("	`oms`.md_busi_data AS mbd\r\n")
						.append("LEFT JOIN `oms-base`.result_legal_person AS rlp ON rlp.busi_id = mbd.busi_id\r\n")
						.append("AND rlp.legal_person_id = ?\r\n")
						.append("WHERE\r\n")
						.append("	instr(mbd.data_use, '2') > 0\r\n")
						.append("AND mbd.busi_type_node = ?\r\n")
						.append("AND (\r\n")
						.append("	mbd.interface_name IS NOT NULL\r\n")
						.append("	OR rlp.busi_value IS NOT NULL\r\n")
						.append(")");

		result = jdbcManager.executeQuery(this.getDBConnection(Constant.SOURCE_CODE_BASESOURCE), querySQL.toString(),
				obj);

		return result;
	}

	/**
	 * 查询信用报告详细信息
	 * 
	 * @param dataSourceDO  数据源
	 * @param legalPersonId 法人ID
	 * @param busiSqldet    数据查询SQL
	 * @return
	 */
	public List<Map<String, Object>> queryReportList(DataSourceDO dataSourceDO, String legalPersonId,
			String busiSqldet) {
		JdbcManager jdbcManager = new JdbcManager();

		DruidPooledConnection conn = null;
		Object[] obj = { legalPersonId };
		try {
			conn = this.getDBConnection(dataSourceDO.getSourceCode());
		} catch (Exception e) {
			logger.error("获取数据库连接出错", e);
		}
		List<Map<String, Object>> list = jdbcManager.executeQuery(conn, busiSqldet, obj);

		return list;
	}

	/**
	 * 保存快照
	 * 
	 * @param legalPersonId 法人ID
	 * @param batch         批次号
	 * @param type          报告类型
	 * @param content       快照详细信息
	 * @param QRCode        二维码信息
	 * @return
	 * @throws Exception
	 */
	public boolean save(String legalPersonId,String batch, int type, String content,String QRCode) throws Exception {
		JdbcManager jdbcManager = new JdbcManager();

		Object[] obj = { legalPersonId, batch, type, content, QRCode };
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO result_snap_shot (\r\n")
			.append("	ss_person_id,\r\n")
			.append("	ss_batch,\r\n")
			.append("	ss_type,\r\n")
			.append("	ss_content,\r\n")
			.append("   ss_QRCode\r\n")
			.append(")\r\n")
			.append("VALUES\r\n")
			.append("	(?, ?, ?, ?, ?)");
		boolean flag = jdbcManager.executeSQL(this.getDBConnection(Constant.SOURCE_CODE_BASESOURCE), sql.toString(),
				obj);
		return flag;
	}

	/**
	 * 查询快照列表
	 * 
	 * @param legalPersonId 法人ID
	 * @param limit         分页参数
	 * @param offset        分页参数
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> listSnapshot(String legalPersonId, int limit, int offset) throws Exception {
		JdbcManager jdbcManager = new JdbcManager();

		Object[] obj = { legalPersonId, offset, limit };
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT\r\n")
			.append("	ss_id,\r\n")
			.append("	ss_batch,\r\n")
			.append("	ss_type\r\n")
			.append("FROM\r\n")
			.append("	result_snap_shot\r\n")
			.append("WHERE\r\n")
			.append("	ss_person_id = ?\r\n")
			.append("ORDER BY\r\n")
			.append("	ss_id DESC\r\n")
			.append("LIMIT ? , ?");
		List<Map<String, Object>> result = jdbcManager.executeQuery(this.getDBConnection(Constant.SOURCE_CODE_BASESOURCE), sql.toString(),
				obj);
		return result;
	}

	/**
	 * 查询快照
	 * 
	 * @param ssId 快照ID
	 * @return
	 * @throws Exception
	 */
	public List<Map<String, Object>> viewSnapShot(String ssId) throws Exception {
		JdbcManager jdbcManager = new JdbcManager();

		Object[] obj = { ssId };
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT\r\n")
			.append("	ss_batch,\r\n")
			.append("	ss_content,\r\n")
			.append("	ss_QRCode\r\n")
			.append("FROM\r\n")
			.append("	result_snap_shot\r\n")
			.append("WHERE\r\n")
			.append("	ss_id = ?\r\n");
		List<Map<String, Object>> result = jdbcManager.executeQuery(this.getDBConnection(Constant.SOURCE_CODE_BASESOURCE), sql.toString(),
				obj);
		return result;
	}

	/**
	 * 查询基础信息
	 * 
	 * @param legalPersonId 法人ID
	 * @param tableName 基础数据表名称
	 * @param SourceCode 数据源编码
	 * @return
	 */
	public LegalPersonReportBaseVO queryBaseData(String legalPersonId,String tableName, String sourceCode) {
		JdbcManager jdbcManager = new JdbcManager();
		Object[] obj = { legalPersonId };
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT\r\n")
				.append("	F01,\r\n")
				.append("	F04,\r\n")
				.append("	F05,\r\n")
				.append("	F12,\r\n")
				.append("	F15,\r\n")
				.append("	F21,\r\n")
				.append("	F19\r\n")
				.append("FROM\r\n")
				.append(tableName +" \r\n")
				.append("WHERE\r\n")
				.append("	LEGAL_PERSON_ID = ?\r\n");
		List<Map<String, Object>> result = null;
		try {
			result = jdbcManager.executeQuery(this.getDBConnection(sourceCode), sql.toString(),
					obj);
		} catch (Exception e) {
			logger.error("获取连接失败",e);
		}
		
		LegalPersonReportBaseVO vo = new LegalPersonReportBaseVO();
		if(result.size()>0) {
			Map<String,Object> map = result.get(0);
			vo.setCompanyName((String) map.get("f01"));
			vo.setRegisteredAddress((String) map.get("f04"));
			vo.setLegalPersonName((String) map.get("f05"));
			vo.setRegisteredCapital((String) map.get("f12"));
			vo.setCreatedDate((String) map.get("f15"));
			vo.setIndustry((String) map.get("f21"));
			vo.setPhoneNumber((String) map.get("f19"));
		}
		return vo;
	}
}
